from pathlib import Path

from PySide6.QtWidgets import QFrame, QButtonGroup, QVBoxLayout, QHBoxLayout, QApplication
from PySide6.QtGui import QResizeEvent
from PySide6.QtCore import Qt, Signal
from qfluentwidgets import CheckBox, TransparentToolButton, TitleLabel, LineEdit, LineEditButton, FluentIcon, \
    PushButton, InfoBar, StateToolTip

from common import EnOrDeCryptThread
from components import TableFrame, OpenFileDialog
from ..base import ReturnView


class PdfEnOrDeCryptView(ReturnView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.__initUI()
        self.__initLayout()
        self.__initEvent()

    def __initUI(self):
        self.returnButton = TransparentToolButton(FluentIcon.RETURN, self)
        self.returnButton.setFixedSize(40, 40)

        self.titleLabel = TitleLabel('PDF文件加密解密', self)
        self.tableWidget = TableFrame(self)
        self.tableWidget.setHeaders(['文件名称', '完成路径'])

        self.checkBox = CheckBox('加密', self)
        self.checkBox.setChecked(True)
        self.checkBox.setFixedHeight(30)
        self.checkBox_2 = CheckBox('解密', self)
        self.checkBox_2.setFixedHeight(30)
        self.pwdLineEdit = LineEdit(self)
        self.pwdLineEdit.setFixedHeight(30)
        self.pwdLineEdit.setPlaceholderText('请输入密码')
        self.lineEdit = LineEdit(self)
        self.lineEdit.setReadOnly(True)
        self.lineEditButton = LineEditButton(FluentIcon.FOLDER, self.lineEdit)
        self.lineEdit.hBoxLayout.addWidget(self.lineEditButton)
        self.lineEdit.setFixedHeight(30)
        self.lineEdit.setPlaceholderText('PDF文件保存路径')
        self.addButton = PushButton(FluentIcon.ADD, '添加文件', self)
        self.addButton.setFixedSize(150, 30)
        self.startButton = PushButton('开始加密/解密', self)
        self.startButton.setFixedHeight(32)

        self.crypt_thread = EnOrDeCryptThread(self)
        self.stateToolTip = StateToolTip('加密/解密', '正在对PDF进行加密/解密中，请稍后...', self)
        self.stateToolTip.hide()

    def __initLayout(self):
        self.buttonGroup = QButtonGroup(self)
        self.buttonGroup.addButton(self.checkBox)
        self.buttonGroup.addButton(self.checkBox_2)

        self.hBox1 = QHBoxLayout()
        self.hBox1.setContentsMargins(0, 0, 0, 0)
        self.hBox1.setSpacing(10)
        self.hBox1.setAlignment(Qt.AlignmentFlag.AlignLeft)

        self.hBox1.addWidget(self.checkBox)
        self.hBox1.addWidget(self.checkBox_2)
        self.hBox1.addWidget(self.pwdLineEdit)
        self.hBox1.addWidget(self.lineEdit)
        self.hBox1.addWidget(self.addButton)

        self.vBox.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignHCenter)
        self.vBox.addWidget(self.tableWidget)
        self.vBox.addLayout(self.hBox1)
        self.vBox.addWidget(self.startButton)

    def __initEvent(self):
        self.returnButton.clicked.connect(self.returnButton_event)
        self.lineEditButton.clicked.connect(self.save_file_event)
        self.addButton.clicked.connect(self.addButton_event)
        self.startButton.clicked.connect(self.startButton_event)

        self.crypt_thread.started.connect(self.started_thread)
        self.crypt_thread.error.connect(self.error_thread)
        self.crypt_thread.finished.connect(self.finished_thread)

    def save_file_event(self):
        file = OpenFileDialog(self).getExistingDirectory()
        self.lineEdit.setText(file)

    def addButton_event(self):
        files = OpenFileDialog(self).getOpenFileNames(filter='PDF文件(*.pdf)')[0]
        for file in files:
            self.tableWidget.addItem([Path(file).name, file])

    def startButton_event(self):
        columnValues = self.tableWidget.getColumnValues(1)
        is_encrypt = self.checkBox.isChecked()
        is_decrypt = self.checkBox_2.isChecked()
        password = self.pwdLineEdit.text()
        save_path = self.lineEdit.text()
        if len(columnValues) == 0 or password == '' or save_path == '':
            InfoBar.warning('提示', '请输入或选择相应数据', duration=3000, parent=self)
            return

        self.crypt_thread.set_value(
            columnValues=columnValues,
            is_encrypt=is_encrypt,
            is_decrypt=is_decrypt,
            password=password,
            save_path=save_path
        )
        self.crypt_thread.start()

    def started_thread(self):
        self.stateToolTip.show()

    def error_thread(self, msg):
        InfoBar.error('错误', msg, duration=5000, parent=self)

    def finished_thread(self):
        self.stateToolTip.hide()
        InfoBar.success('成功', '加密/解密成功', duration=3000, parent=self)

    def returnButton_event(self):
        self.tableWidget.clear()
        self.pwdLineEdit.clear()
        self.lineEdit.clear()
        self.returned.emit()

    def resizeEvent(self, event: QResizeEvent):
        self.stateToolTip.move(event.size().width() - self.stateToolTip.width() - 20, 10)
        super().resizeEvent(event)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    win = PdfEnOrDeCryptView()
    win.resize(800, 600)
    win.show()
    sys.exit(app.exec())
